package com.example.about_netty.channelhandler;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import lombok.SneakyThrows;

@ChannelHandler.Sharable
public class SharableMessageHandler extends SimpleChannelInboundHandler<Message> {


    @SneakyThrows
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Message message) {
        if (message instanceof SuccessMsg) {
            AttributeKey<Object> attributeKey = AttributeKey.valueOf("status");
            Attribute<Object> attr = ctx.attr(attributeKey); //不推荐 可以通过context绑定的channel来直接获取
            attr.set(true);

            Attribute<Object> attribute = ctx.channel().attr(attributeKey);
            attribute.set(true);
        } else if (message instanceof FailedMsg) {
            ctx.writeAndFlush(message);
        } else {
            throw new Exception("消息错误");
        }


    }

}
